<?php

    class IcheckerController extends WebController
    {
        public $menu_selected = 'tool';

        public function actionIndex(){
            $this->pageTitle = "iChecker - Check your apple devices";
            $this->pageDesc = "iChecker help you quickly check all Apple devices: iphone, ipad, macbook, ipad and more by serial number or imei.";
            $this->ogImage = $this->baseUrl.'/files/app/ichecker.jpg';
            $cs = Yii::app()->getClientScript();
            $cs->registerScriptFile($this->baseUrl.'/files/app/ichecker.js');
            $this->render('index');
        }

        public function actionApi(){
            error_reporting(0);
            $referer = Yii::app()->request->urlReferrer;
            if(strpos($referer, 'ichecker') === FALSE) $this->redirect(array('/ichecker'));

            $q = trim(Yii::app()->request->getPost('q'));

            Yii::import('ext.Curl');
            Yii::import('ext.simple_html_dom');

            //http://s-url.ro/
            //http://89.137.253.14/2012.php?imei=012799008365648
            //http://89.137.253.14/Iast.php?imei=DQGJ53GKDTD7
            $icurl = new Curl('http://89.137.253.14/Iast.php?imei='.$q);
            $icurl->referer = 'http://www.s-url.ro/';

            //set_time_limit(1);
            $content = $icurl->run();
            //        echo "<pre>";print_r($content);echo "</pre>";die;
            $html = new simple_html_dom($content);
            $content = $html->find('#rezultat', 0)->innertext;

            //echo "<pre>";print_r($content);echo "<pre>";die;

            //        $content = '<center><img src="https://km.support.apple.com.edgekey.net/kb/securedImage.jsp?configcode=DKQ1&size=72x72"/></center><br />
            //<div id="bax">&nbsp<b>Model:</b> MacBook Pro (Retina, Mid 2012) </div>
            //<div id="bax">&nbsp<b>Device ID1:</b> C02J8AETDKQ1</div>
            //<div id="bax">&nbsp<b>Device ID2: </b>null</div>
            //<div id="bax">&nbsp<b>Repairs & Service Coverage:</b> Active</div>
            //<div id="bax">&nbsp<b>Telephone Technical Support: </b>Expired</div>
            //<div id="bax">&nbsp<b>AppleCare Protection Plan: </b>Eligible</div>
            //<div id="bax">&nbsp<b>Warranty Expires: </b>August 31, 2013<br/></div><div id="bax">&nbsp<b>Apple Store Downloads:</b> False</div>
            //<div id="bax">&nbsp<b>Warranty Type:</b> Limited Warranty</div>
            //<div id="bax">&nbsp<b>Purchase Date:</b> Validated</div>
            //<div id="bax">&nbsp<b>Registered:</b> Yes</div>
            //<div id="bax">&nbsp<b>Activated:</b> Yes</div>
            //<center><div style="overflow:hidden;" class="fb-like" data-href="http://www.s-url.ro" data-layout="button_count" data-width="110" data-show-faces="false" data-font="verdana"></div></center><center><b>Check carrier for Free, <a href="http://imei.s-url.ro" target="_blank"><u>Click here!</u></a></b></center><br />

            //<center><b>Unlock AT&T iPhone instant (1 minute) contact <a href="mailto:surlro@yahoo.com" target="_blank"><u>surlro@yahoo.com</u></a></b></center>';

            $html = new simple_html_dom($content);
            $img = $html->find('img', 0)->src;
            $name = NULL;
            $attrs = array();
            foreach($html->find('div') as $div){
                $s = $div->innertext;
                preg_match('{.*<b>(.+)</b>(.+)}', $s, $m);

                $k = trim($m[1], ' :');
                $v = trim(strip_tags($m[2]));
                if($k == 'Model') $name = $v;
                if($k == 'Device ID1') $k = 'Device ID';



                if($v && $v != 'null'){
                    $attrs[] = array(
                        'key' => $k,
                        'value' => $v,
                    );
                }

            }

            $data = 'invalid';
            if($img && $name && $attrs){
                $data = array(
                    'name' => $name,
                    'img' => $img,
                    'attrs' => $attrs,
                );
            }

            echo json_encode($data);
        }

    }
